package slw.controls
{
	import flash.display.DisplayObject;
	import flash.display.GradientType;
	import flash.display.Shape;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	
	import org.osmf.events.TimeEvent;
	
	import slw.core.UIComponent;
	import slw.events.ScrollEvent;
	
	
	/**
	 * ScrollBar
	 * 滚动条类
	 * @author ddx<br/>
	 * 2011-8-2
	 *
	 */
	public class ScrollBar extends UIComponent
	{
		/**
		 *  "horizontal"滚动条的方向为水平
		 */		
		public static const HORIZONTAL:String="horizontal";
		/**
		 *  "vertical"滚动条的方向为竖直
		 */		
		public static const VERTICAL:String="vertical";
		//显示范围，滚动位置，最小滚动值，最大滚动值，滚动增量，滚动百公比
		private var _pageSize:Number=0;
		private var _scrollPosition:Number=0;
		private var _minScrollPosition:Number=0;
		private var _maxScrollPosition:Number=0;
		private var _scrollSize:Number=0;
		private var _scrollPercent:Number=0;
		//箭头默认尺寸(与滚动条方向垂直轴的尺寸)，滚动条方向默认尺寸，拖条最小尺寸
		private var _arrowSize:Number=15;
		private var _totalSize:Number=150;
		private var _minThumbSize:Number=8;
		//滚动条方向
		private var _direction:String;
		//轨道，上箭头，下箭头，拖条
		private var _tracker:BaseButton;
		private var _upArrow:BaseButton;
		private var _downArrow:BaseButton;
		private var _thumb:BaseButton;
		//上4个部件数组
		private var _parts:Array=[];
		//皮肤数组
		private var _skin:Array=[];
		//连续按下轨道，上箭头，下箭头时滚动计时器
		private var _timer:Timer;
		//按下轨道，上箭头，下箭头时计时器，
		private var _timeoutTimer:Timer;
		//按下轨道，上箭头，下箭头时调度滚动计时器开始的延迟
		private var _downDelay:Number=300;
		//连续滚动间隔
		private var _scrollDelay:Number=50;
		//按下的部件
		private var _activeTarget:DisplayObject;
		
		//方向相关的属性数组级索引
		private var _axises:Array=["x","y"];
		private var _sizes:Array=["width","height"];
		private var _reverse:Array=["height","width"];
		private var _mouseAxises:Array=["mouseX","mouseY"];
		//水平:0，竖直:1
		private var _index:int=0;
		/**
		 *  构造函数
		 * @param direction:String=VERTICAL 滚动条方向
		 * @param pageSize:Number=100 滚动条显示范围
		 * @param scrollSize:Number=10 滚动增量(像素)
		 * @param minScrollPosition:Number=0 最小滚动范围(像素或相对值)
		 * @param maxScrollPosition:Number=100 最大滚动范围(像素或相对值)
		 * 
		 */		
		public function ScrollBar(direction:String=VERTICAL,pageSize:Number=100,scrollSize:Number=10,minScrollPosition:Number=0,maxScrollPosition:Number=100)
		{
			super();
			this.listName="ScrollBar";
			
			init();
			
			this.round=5;
			this.direction=direction;
			setProperties(pageSize,scrollSize,minScrollPosition,maxScrollPosition);
			
			setSize(_arrowSize,_totalSize);
		}
		
		/*
		-----------------------------------
		setters getters
		-----------------------------------
		*/
		//
		/**
		 * 滚动条方向，可能的值:<br/>
		 * ScrollBar.HORIZONTAL:水平方向<br/>
		 * ScrollBar.VERTICAL:竖直方向，默认值<br/> 
		 * @param value:String
		 * 
		 */		
		public function set direction(value:String):void{
			if(_direction==value) return;
			
			_direction=value;
			_index=[HORIZONTAL,VERTICAL].indexOf(_direction);
			if(_index==-1)
				throw new Error("direction属性可能的值为以下之一:"+[HORIZONTAL,VERTICAL]);
			
			resetPosition();
			var $max:Number=Math.max(this.width,this.height);
			var $min:Number=Math.min(this.width,this.height);
			if(_direction==HORIZONTAL){
				//setRotation(-90);
				setSize($max,$min);
			}else if(_direction==VERTICAL){
				//setRotation(0);
				setSize($min,$max);
			}
		}
		//
		/**
		 * 滚动条方向，可能的值:<br/>
		 * ScrollBar.HORIZONTAL:水平方向<br/>
		 * ScrollBar.VERTICAL:竖直方向，默认值<br/> 
		 * @param value:String
		 * 
		 */
		public function get direction():String{
			return _direction;
		}
		//
		/**
		 * 当前滚动位置(相对值), 介于最小滚动范围和最大滚动范围之间
		 * @param value:Number
		 * 
		 */		
		public function set scrollPosition(value:Number):void{
			if(_scrollPosition==value) return;
			
			_scrollPosition=value;
			_scrollPosition=Math.min(_scrollPosition,_maxScrollPosition);
			_scrollPosition=Math.max(_scrollPosition,_minScrollPosition);
			
			_scrollPercent=_scrollPosition/(_maxScrollPosition-_minScrollPosition);
			
			updateDisplayList();
			
			this.dispatchEvent(new ScrollEvent(ScrollEvent.SCROLL,true,false,_scrollPosition,_scrollPercent));
		}
		//
		/**
		 * 当前滚动位置(相对值), 介于最小滚动范围和最大滚动范围之间
		 * @param value:Number
		 * 
		 */
		public function get scrollPosition():Number{
			return _scrollPosition;
		}
		//
		public function set scrollPercent(value:Number):void{
			if(_scrollPercent==value) return;
			
			_scrollPercent=value;
			_scrollPercent=Math.max(_scrollPercent,0);
			_scrollPercent=Math.min(_scrollPercent,1);
			
			this.scrollPosition=_minScrollPosition+_scrollPercent*(_maxScrollPosition-_minScrollPosition);
		}
		//
		public function get scrollPercent():Number{
			return _scrollPercent;
		}
		//
		/**
		 * 滚动增量(相对值)，按下轨道，上箭头，下箭头时scrollPosition 增加或减少的量
		 * @param value:Number
		 * 
		 */		
		public function set scrollSize(value:Number):void{
			if(_scrollSize==value) return;
			
			_scrollSize=value;
			
			updateDisplayList();
		}
		//
		/**
		 * 滚动增量(相对值)，按下轨道，上箭头，下箭头时scrollPosition 增加或减少的量
		 * @param value:Number
		 * 
		 */
		public function get scrollSize():Number{
			return _scrollSize;
		}
		//
		/**
		 * 滚动条显示范围，当值处于minScrollPosition-minScrollPosition之间时，拖条可滚动
		 * @param value:Number
		 * 
		 */
		public function set pageSize(value:Number):void{
			if(_pageSize==value) return;
			
			_pageSize=value;
			_pageSize=Math.max(_pageSize,1);
			
			updateThumbSize();
			updateDisplayList();
		}
		//
		/**
		 * 滚动条显示范围，当值处于minScrollPosition-minScrollPosition之间时，拖条可滚动
		 * @param value:Number
		 * 
		 */
		public function get pageSize():Number{
			return _pageSize;
		}
		//
		/**
		 * 最小滚动值,拖条位于最顶端时scrollPosition的值 
		 * @param value:Number
		 * 
		 */		
		public function set minScrollPosition(value:Number):void{
			if(_minScrollPosition==value) return;
			
			_minScrollPosition=value;
			_minScrollPosition=Math.min(_minScrollPosition,_maxScrollPosition);
			
			updateThumbSize();
			updateDisplayList();
		}
		//
		/**
		 * 最小滚动值,拖条位于最顶端时scrollPosition的值  
		 * @param value:Number
		 * 
		 */
		public function get minScrollPosition():Number{
			return _minScrollPosition;
		}
		//
		/**
		 * 最大滚动值,拖条位于最底时scrollPosition的值  
		 * @param value:Number
		 * 
		 */		
		public function set maxScrollPosition(value:Number):void{
			if(_maxScrollPosition==value) return;
			
			_maxScrollPosition=value;
			
			updateThumbSize();
			updateDisplayList();
		}
		//
		/**
		 * 最大滚动值,拖条位于最顶端时scrollPosition的值  
		 * @param value:Number
		 * 
		 */
		public function get maxScrollPosition():Number{
			return _maxScrollPosition;
		}
		//
		/**
		 * 组件是否激活,默认值 true(激活)
		 * @param value:Boolean
		 * 
		 */			
		override public function set enabled(value:Boolean):void{
			if(this.enabled==value) return;
			
			super.enabled=value;
			
			for(var i:int=0;i<_parts.length;i++){
				BaseButton(_parts[i]).enabled=value;
			}
			_thumb.visible=this.enabled;
		}
		//
		/**
		 * 组件是否激活,默认值 true(激活)
		 * @param value:Boolean
		 * 
		 */	
		override public function set round(value:Number):void{
			if(this.round==value) return;
			
			super.round=value;
			
			for(var i:int=0;i<_parts.length;i++){
				BaseButton(_parts[i]).round=value;
			}
			setDefaultSkin();
		}
		//
		/**
		 * 2维皮肤数组,数组中的4个1维数组(元素为影片字符串类名)分别设置轨道，上箭头，下箭头，拖条的皮肤,如:[
					["TrackSkin"],
					["UpArrowSkin"],
					["DownArrowSkin"],
					["ThumbSkin"]
				]
				 * (目前不支持影片垂直皮肤用于水平滚动条,若使用水平滚动条并使用自定义皮肤，皮肤需绘制为水平方向)
		 * @param value:Array
		 * 
		 */		
		override public function set skin(value:Array):void{
			if(_skin==value) return;
			
			_skin=value;
			
			for(var i:int=0;i<_parts.length;i++){
				BaseButton(_parts[i]).displayStates=null;
				BaseButton(_parts[i]).skin=_skin[i];
			}
		}
		//
		/**
		 * 2维皮肤数组,数组中的4个1维数组(元素为影片字符串类名)分别设置轨道，上箭头，下箭头，拖条的皮肤,如:[
					["TrackSkin"],
					["UpArrowSkin"],
					["DownArrowSkin"],
					["ThumbSkin"]
				]
				 * (目前不支持影片垂直皮肤用于水平滚动条,若使用水平滚动条并使用自定义皮肤，皮肤需绘制为水平方向)
		 * @param value:Array
		 * 
		 */
		override public function get skin():Array{
			return _skin;
		}
		//
		/**
		 * 不支持 
		 * 
		 */		
		override public function set displayURLs(value:Array):void{
			
		}
		//
		/**
		 * 不支持
		 * 
		 */			
		override public function set autoSizeBySkin(value:Boolean):void{
			
		}
		//
		/**
		 * 组件宽度，默认值 15
		 * @param value:Number
		 * 
		 */	
		override public function set width(value:Number):void{
			if(this.width==value) return;
			
			if(_direction==HORIZONTAL){
				value=Math.max(value,_arrowSize*2);
				super[_sizes[_index]]=value;
				setDirectionAxis(value);
			}else if(_direction==VERTICAL){
				value=_arrowSize;
				super[_reverse[_index]]=value;
				setVerticalAxis(value);
			}
			
			setDefaultSkin();
		}
		//
		/**
		 * 组件高度，默认值 150 
		 * @param value:Number
		 * 
		 */
		override public function set height(value:Number):void{
			if(this.height==value) return;
			
			if(_direction==HORIZONTAL){
				value=_arrowSize;
				super[_reverse[_index]]=value;
				setVerticalAxis(value);
			}else if(_direction==VERTICAL){
				value=Math.max(value,_arrowSize*2);
				super[_sizes[_index]]=value;
				setDirectionAxis(value);
			}
			
			setDefaultSkin();
		}
		/*
		-----------------------------------
		public  methods
		-----------------------------------
		*/
		//
		/**
		 * 设置滚动条属性 
		 * @param pageSize:Number=100 滚动条显示范围
		 * @param scrollSize:Number=10 滚动增量(像素)
		 * @param minScrollPosition:Number=0 最小滚动范围(像素或相对值)
		 * @param maxScrollPosition:Number=100 最大滚动范围(像素或相对值)
		 * 
		 */		
		public function setProperties(pageSize:Number=100,scrollSize:Number=10,minScrollPosition:Number=0,maxScrollPosition:Number=100):void{
			_pageSize=pageSize;
			_minScrollPosition=minScrollPosition;
			_maxScrollPosition=maxScrollPosition;
			_scrollSize=scrollSize;
			
			updateThumbSize();
			updateDisplayList();
		}
		/**
		 * 销毁对象实例，并释放资源(从显示列表删除，删除所有侦听)
		 * 
		 */		
		override public function destroy():void{
			removeListeners();
			if(this.parent!=null){
				this.parent.removeChild(this);
			}
		}
		/*
		-----------------------------------
		private  methods
		-----------------------------------
		*/
		//初始化
		private function init():void{
			this.mouseChildren=true;
			
			_tracker=new BaseButton();
			this.buildContainer.addChild(_tracker);
			
			_upArrow=new BaseButton();
			this.buildContainer.addChild(_upArrow);
			
			_downArrow=new BaseButton();
			this.buildContainer.addChild(_downArrow);
			
			_thumb=new BaseButton();
			this.buildContainer.addChild(_thumb);
			
			_parts=[_tracker,_upArrow,_downArrow,_thumb];
			
			addListeners();
		}
		//添加侦听
		private function addListeners():void{
			_tracker.addEventListener(MouseEvent.MOUSE_DOWN,trackerMouseDown);
			_upArrow.addEventListener(MouseEvent.MOUSE_DOWN,upArrowMouseDown);
			_downArrow.addEventListener(MouseEvent.MOUSE_DOWN,downArrowMouseDown);
			_thumb.addEventListener(MouseEvent.MOUSE_DOWN,thumbMouseDown);
		}
		//删除侦听
		private function removeListeners():void{
			_tracker.removeEventListener(MouseEvent.MOUSE_DOWN,trackerMouseDown);
			_upArrow.removeEventListener(MouseEvent.MOUSE_DOWN,upArrowMouseDown);
			_downArrow.removeEventListener(MouseEvent.MOUSE_DOWN,downArrowMouseDown);
			_thumb.removeEventListener(MouseEvent.MOUSE_DOWN,thumbMouseDown);
		}
		//更新显示
		private function updateDisplayList():void{
			_thumb[_axises[_index]]=getPadding()+getPositionPercent()*getSlideSize();
		}
		//拖条最小位置
		private function getPadding():Number{
			return _upArrow[_sizes[_index]];
		}
		//拖条位置百分比(由滚动位置计算)
		private function getPositionPercent():Number{
			var $percent:Number=Math.abs(_scrollPosition)/getPageTotalSize();
			$percent=Math.min($percent,1);
			$percent=Math.max($percent,0);
			
			return $percent;
		}
		//拖条位置百分比(由拖条位置计算)
		private function getPositionPercentByThumb():Number{
			var $percent:Number=(_thumb[_axises[_index]]-getPadding())/getSlideSize();
			$percent=isNaN($percent)?0:$percent;
			$percent=Math.min($percent,1);
			$percent=Math.max($percent,0);
			
			return $percent;
		}
		//显示范围与滚动范围百分比
		private function getPagePercent():Number{
			var $percent:Number=_pageSize/getPageTotalSize();
			$percent=Math.min($percent,1);
			$percent=Math.max($percent,0);
			
			return $percent;
		}
		//滚动条总共可用范围
		private function getTrackerSize():Number{
			var $size:Number=this[_sizes[_index]]-_upArrow[_sizes[_index]]-_downArrow[_sizes[_index]];
			return $size<0?0:$size;
		}
		//拖条可滚动范围
		private function getSlideSize():Number{
			var $size:Number=this[_sizes[_index]]-_thumb[_sizes[_index]]-_upArrow[_sizes[_index]]-_downArrow[_sizes[_index]];
			return $size<0?0:$size;
		}
		//滚动条总共可用范围相对值
		private function getPageTotalSize():Number{
			var $size:Number=_maxScrollPosition-_minScrollPosition;
			$size=Math.max($size,1);
			return $size;
		}
		//滚动增加计算相对值
		private function getRelativeScrollSize():Number{
			return _scrollSize/getTrackerSize()*getPageTotalSize();
		}
		//拖条大小
		private function updateThumbSize():void{
			_thumb[_sizes[_index]]=getPagePercent()*getTrackerSize();
			_thumb[_sizes[_index]]=Math.max(_thumb[_sizes[_index]],_minThumbSize);
			
			buildThumbStates();
		}
		//开始连续滚动计时
		private function startTimer():void{
			if(_timer==null)
				_timer=new Timer(_scrollDelay);
			_timer.addEventListener(TimerEvent.TIMER,timerHandler);
			_timer.start();
		}
		//停止连续滚动计时
		private function stopTimer():void{
			if(_timer==null) return;
			
			_timer.stop();
			_timer.removeEventListener(TimerEvent.TIMER,timerHandler);
		}
		//开始连续滚动计时延迟
		private function setTimeout(delay:Number=0,closure:Function=null):void{
			if(_timeoutTimer!=null){
				_timeoutTimer.stop();
				_timeoutTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,completeHandler);
			}
			if(closure==null) return;
			
			_timeoutTimer=new Timer(delay,1);
			_timeoutTimer.addEventListener(TimerEvent.TIMER_COMPLETE,completeHandler);
			_timeoutTimer.start();
			
			function completeHandler(e:TimerEvent):void{
				if(closure!=null) closure();
				
				_timeoutTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,completeHandler);
			}
		}
		//滚动位置是否到达最小或最大值
		private function checkPosition():Boolean{
			if(getPositionPercentByThumb()==1||getPositionPercentByThumb()==0){
				return true;
			}
			return false;
		}
		//默认皮肤
		private function setDefaultSkin():void{
			if(this.skin!=null&&this.skin.length!=0) return;
			
			buildThumbStates();
			
			var $states:Array=[];
			var $borderColors:Array=[0x999999,0x0099CC,0x009999,0x999999];
			var $colors:Array=[[0xeeeeee,0xcccccc],[0xeeeeee,0x85C9E0],[0x85C9E0,0xeeeeee],[0xcccccc,0xeeeeee]];
			var $state:Shape;
			var $rotation:Number;
			var $target:BaseButton=_upArrow;
			
			if(_direction==HORIZONTAL){
				$rotation=0;
			}else if(_direction==VERTICAL){
				$rotation=Math.PI/2;
			}
			for(var i:int=0;i<4;i++){
				$state=getState($borderColors[i],$colors[i],$target.width,$target.height,this.round,$rotation) as Shape;
				drawArrow($state,0x666666,"up");
				$states.push($state);
			}
			$target.displayStates=$states;
			
			$states=[];
			$target=_downArrow;
			if(_direction==HORIZONTAL){
				$rotation=0;
			}else if(_direction==VERTICAL){
				$rotation=Math.PI/2;
			}
			for(i=0;i<4;i++){
				$state=getState($borderColors[i],$colors[i],$target.width,$target.height,this.round,$rotation) as Shape;
				drawArrow($state,0x666666,"down");
				$states.push($state);
			}
			$target.displayStates=$states;
			
			$states=[];
			$borderColors=[0xbbbbbb,0xbbbbbb,0xbbbbbb,0x999999];
			$colors=[[0xcccccc,0xeeeeee],[0xcccccc,0xeeeeee],[0xcccccc,0xeeeeee],[0xcccccc,0xeeeeee]];
			$target=_tracker;
			if(_direction==HORIZONTAL){
				$rotation=Math.PI/2;
			}else if(_direction==VERTICAL){
				$rotation=0;
			}
			for(i=0;i<4;i++){
				$state=getState($borderColors[i],$colors[i],$target.width,$target.height,this.round,$rotation) as Shape;
				$states.push($state);
			}
			$target.displayStates=$states;
		}
		//拖条默认皮肤
		private function buildThumbStates():void{
			if(this.skin!=null&&this.skin.length!=0) return;
			
			var $states:Array=[];
			var $borderColors:Array=[0x999999,0x0099CC,0x009999,0x999999];
			var $colors:Array=[[0xeeeeee,0xcccccc],[0xeeeeee,0x85C9E0],[0x85C9E0,0xeeeeee],[0xcccccc,0xeeeeee]];
			var $state:Shape;
			var $rotation:Number;
			if(_direction==HORIZONTAL){
				$rotation=Math.PI/2;
			}else if(_direction==VERTICAL){
				$rotation=0;
			}
			for(var i:int=0;i<4;i++){
				$state=getState($borderColors[i],$colors[i],_thumb.width,_thumb.height,this.round,$rotation) as Shape;
				drawThumbLine($state,0x999999);
				$states.push($state);
			}
			
			_thumb.displayStates=$states;
		}
		//绘制按钮状态
		private function getState(borderColor:uint,colors:Array,width:Number,height:Number,round:Number,rotation:Number=Math.PI/2):DisplayObject{
			var $m:Matrix=new Matrix();
			$m.createGradientBox(width,height,rotation);
			
			var $s:Shape=new Shape();
			$s.graphics.lineStyle(0,borderColor,1,true,"none","round");
			$s.graphics.beginGradientFill(GradientType.LINEAR,colors,[1,1],[0,0xff],$m);
			$s.graphics.drawRoundRect(0,0,width,height,round,round);
			$s.graphics.endFill();
			
			return $s;
		}
		//绘制上下箭头
		private function drawArrow(target:Shape,color:uint,direction:String):void{
			var $thickness:int=1;
			var $offset:int=_arrowSize*1/4;
			
			target.graphics.lineStyle($thickness,color,0,false,"none","round");
			target.graphics.beginFill(color,1);
			
			var $point1s:Array=[
				[target[_reverse[_index]]/2+$thickness/2,target[_sizes[_index]]*1/3],//上中
				[target[_reverse[_index]]-$offset,target[_sizes[_index]]*2/3],//上右
				[$offset+$thickness,target[_sizes[_index]]*2/3],//上左
				[target[_reverse[_index]]/2+$thickness/2,target[_sizes[_index]]*2/3],//下中
				[target[_reverse[_index]]-$offset,target[_sizes[_index]]*1/3],//下右
				[$offset+$thickness,target[_sizes[_index]]*1/3]//下左
			];
			for(var i:int=0;i<$point1s.length;i++){
				if(_direction==HORIZONTAL){
					$point1s[i]=new Point($point1s[i][1],$point1s[i][0]);
				}else if(_direction==VERTICAL){
					$point1s[i]=new Point($point1s[i][0],$point1s[i][1]);
				}
			}
			
			if(direction=="up"){
				$point1s=$point1s.slice(0,3);
			}else if(direction=="down"){
				$point1s=$point1s.slice(3);
			}
			target.graphics.moveTo($point1s[2].x,$point1s[2].y);
			for(i=0;i<$point1s.length;i++){
				target.graphics.lineTo($point1s[i].x,$point1s[i].y);
			}
			
			target.graphics.endFill();
		}
		//绘制拖条中间图标
		private function drawThumbLine(target:Shape,color:uint):void{
			var $thickness:int=1;
			var $gap:int=1;
			var $offset:int=_arrowSize*1/4;
			var $size:Number=($thickness+$gap)*4-$gap;
			
			if(target[_sizes[_index]]<$size+$gap*4) return;
			
			target.graphics.lineStyle($thickness,color);
			
			var $point1:Point;
			var $point2:Point;
			if(_direction==HORIZONTAL){
				$point1=new Point((target[_sizes[_index]]-$size)/2,$offset);
				$point2=new Point((target[_sizes[_index]]-$size)/2,target[_reverse[_index]]-$offset);
			}else if(_direction==VERTICAL){
				$point1=new Point($offset,(target[_sizes[_index]]-$size)/2);
				$point2=new Point(target[_reverse[_index]]-$offset,(target[_sizes[_index]]-$size)/2);
			}
			if(_direction!=null)
				for(var i:int=0;i<4;i++){
					if(i>0){
						$point1[_axises[_index]]+=($thickness+$gap);
						$point2[_axises[_index]]+=($thickness+$gap);
					}
					
					target.graphics.moveTo($point1.x,$point1.y);
					target.graphics.lineTo($point2.x,$point2.y);
				}
		}
		//设置滚动条方向上的部件属性
		private function setDirectionAxis(value:Number):void{
			_tracker[_sizes[_index]]=value; 
			_upArrow[_sizes[_index]]=_arrowSize;
			_downArrow[_sizes[_index]]=_arrowSize;
			updateThumbSize();
			
			_downArrow[_axises[_index]]=this[_sizes[_index]]-_downArrow[_sizes[_index]];
			
			updateDisplayList();
		}
		//设置与滚动条方向相垂直的方向上的部件属性
		private function setVerticalAxis(value:Number):void{
			for(var i:int=0;i<_parts.length;i++){
				BaseButton(_parts[i])[_reverse[_index]]=value;
			}
		}
		//重置部件位置
		private function resetPosition():void{
			for(var i:int=0;i<_parts.length;i++){
				BaseButton(_parts[i]).x=0;
				BaseButton(_parts[i]).y=0;
			}
		}
		//旋转皮肤
		private function setRotation(rotation:Number):void{
			if(this.skin==null||this.skin.length==0) return;
			for(var i:int=0;i<_parts.length;i++){
				BaseButton(_parts[i]).container.rotation=rotation;
				if(_direction==HORIZONTAL){
					BaseButton(_parts[i]).container.x=0;
					BaseButton(_parts[i]).container.y=BaseButton(_parts[i]).height;
				}else if(_direction==VERTICAL){
					BaseButton(_parts[i]).container.x=0;
					BaseButton(_parts[i]).container.y=0;
				}
			}
		}
		//按下轨道
		private function trackerMouseDown(e:MouseEvent):void{
			_activeTarget=e.target as DisplayObject;
			
			setTimeout(_downDelay,startTimer);
			addStageListener();
		}
		//按下上箭头
		private function upArrowMouseDown(e:MouseEvent):void{
			this.scrollPosition-=getRelativeScrollSize();
			_activeTarget=e.target as DisplayObject;
			
			setTimeout(_downDelay,startTimer);
			addStageListener();
			
			e.updateAfterEvent();
		}
		//按下下箭头
		private function downArrowMouseDown(e:MouseEvent):void{
			this.scrollPosition+=getRelativeScrollSize();
			_activeTarget=e.target as DisplayObject;
			
			setTimeout(_downDelay,startTimer);
			addStageListener();
			
			e.updateAfterEvent();
		}
		//按下拖条
		private function thumbMouseDown(e:MouseEvent):void{
			if(_direction==HORIZONTAL){
				_thumb.startDrag(false,new Rectangle(getPadding(),0,getSlideSize(),0));
			}else if(_direction==VERTICAL){
				_thumb.startDrag(false,new Rectangle(0,getPadding(),0,getSlideSize()));
			}
			
			addStageListener();
			
			if(this.stage!=null)
				this.stage.addEventListener(MouseEvent.MOUSE_MOVE,stageMouseMove);
		}
		//鼠标弹起侦听
		private function addStageListener():void{
			if(this.stage!=null)
				this.stage.addEventListener(MouseEvent.MOUSE_UP,stageMouseUp);
		}
		//鼠标弹起
		private function stageMouseUp(e:MouseEvent):void{
			_thumb.stopDrag();
			stopTimer();
			setTimeout();
			
			_activeTarget=null;
			
			if(this.stage!=null){
				this.stage.removeEventListener(MouseEvent.MOUSE_UP,stageMouseUp);
				this.stage.removeEventListener(MouseEvent.MOUSE_MOVE,stageMouseMove);
			}
		}
		//鼠标移动
		private function stageMouseMove(e:MouseEvent):void{
			_scrollPosition=_minScrollPosition+getPositionPercentByThumb()*getPageTotalSize();
			_scrollPercent=_scrollPosition/(_maxScrollPosition-_minScrollPosition);
			
			e.updateAfterEvent();
			
			this.dispatchEvent(new ScrollEvent(ScrollEvent.SCROLL,true,false,_scrollPosition,_scrollPercent));
		}
		//连续滚动
		private function timerHandler(e:TimerEvent):void{
			switch(_activeTarget){
				case _tracker:
					var $p:Number=(_tracker[_mouseAxises[_index]]-_thumb[_sizes[_index]]/2);
					
					var $distance:Number=$p-_thumb[_axises[_index]];
					var $increment:Number=$distance;
					$increment=Math.abs($increment);
					$increment=$increment/getTrackerSize()*getPageTotalSize();
					$increment=Math.min($increment,getRelativeScrollSize());
					
					if($distance>0&&Math.abs($distance)>1){
						this.scrollPosition+=$increment;
					}else if($distance<0&&Math.abs($distance)>1){
						this.scrollPosition-=$increment;
					}else{
						stopTimer();
					}
					break;
				case _upArrow:
					this.scrollPosition-=getRelativeScrollSize();
					break;
				case _downArrow:
					this.scrollPosition+=getRelativeScrollSize();
					break;
				default:
					stopTimer();
					break;
			}
			
			if(checkPosition()) stopTimer();
			
			e.updateAfterEvent();
		}
	}
}